package com.mzba.place.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.actionbarsherlock.app.SherlockFragment;
import com.mzba.place.R;
import com.mzba.place.ui.MainActivity;
import com.viewpagerindicator.CirclePageIndicator;
import com.viewpagerindicator.PageIndicator;
/**
 * 
 * @author 06peng
 *
 */
public class HelpFragment extends SherlockFragment {

	private MainActivity mainActivity;
	CirclePageFragmentAdapter mAdapter;
	ViewPager mPager;
	PageIndicator mIndicator;
	private LinearLayout mainView;
	private View fragmentView;
	private boolean isInitFragmentFirth;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mainActivity = (MainActivity) getSherlockActivity();
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mainView = (LinearLayout) inflater.inflate(R.layout.activity_main, null);
		initViewpageFragment();
		return mainView;
	}
	
	private void initViewpageFragment() {
		if (!isInitFragmentFirth) {
			fragmentView = LayoutInflater.from(mainActivity).inflate(R.layout.fragment_help, null);
			mAdapter = new CirclePageFragmentAdapter(mainActivity.getSupportFragmentManager(), mainActivity);
			Bundle bundle = new Bundle();
			bundle.putInt("item", 1);
	        mAdapter.addFragment("", HelpBgFragment.class, bundle);
	        
	        bundle = new Bundle();
			bundle.putInt("item", 2);
	        mAdapter.addFragment("", HelpBgFragment.class, bundle);
	        
	        bundle = new Bundle();
			bundle.putInt("item", 3);
	        mAdapter.addFragment("", HelpBgFragment.class, bundle);
	        
	        bundle = new Bundle();
			bundle.putInt("item", 4);
	        mAdapter.addFragment("", HelpBgFragment.class, bundle);
	        
	        bundle = new Bundle();
			bundle.putInt("item", 5);
	        mAdapter.addFragment("", HelpBgFragment.class, bundle);
	        
			mPager = (ViewPager) fragmentView.findViewById(R.id.pager);
			mPager.setAdapter(mAdapter);
			
			mIndicator = (CirclePageIndicator) fragmentView.findViewById(R.id.indicator);
			mIndicator.setViewPager(mPager);
			mainView.addView(fragmentView);
			isInitFragmentFirth = true;
		} else {
			mainView.addView(fragmentView);
		}
    }
	
	@Override
	public void onPause() {
		super.onPause();
		mainView.removeAllViews();
	}
	

	public static class CirclePageFragmentAdapter extends FragmentPagerAdapter {

    	private Context mContext;
		private final ArrayList<FragmentInfo> fragments = new ArrayList<FragmentInfo>();
		
		protected static final class FragmentInfo {
            @SuppressWarnings("unused")
			private final String tag;
            private final Class<?> clss;
            private final Bundle args;

            protected  FragmentInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }
    	
		public CirclePageFragmentAdapter(FragmentManager fm, Context context) {
			super(fm);
			this.mContext = context;
		}
		
		public void addFragment(String tag, Class<?> clss, Bundle args) {
			FragmentInfo fragmentInfo = new FragmentInfo(tag, clss, args);
			fragments.add(fragmentInfo);
		}

		@Override
		public Fragment getItem(int arg0) {
			FragmentInfo fragmentInfo = fragments.get(arg0);
			return Fragment.instantiate(mContext, fragmentInfo.clss.getName(), fragmentInfo.args);
		}

		@Override
		public int getCount() {
			return fragments.size();
		}
    }
	
	
}


